/*
 * Application.java
 * Copyright (C) 2007 Ezequiel Turovetzky
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
package ar.com.botonera;

import java.awt.SystemTray;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import ar.com.botonera.gui.MainWindow;
import ar.com.botonera.gui.systray.BotoneraTray;
import ar.com.botonera.utils.ExceptionHandlerFactory;

/**
 * Representa la aplicaci&oacute;n.
 * <br>
 * Contiene m&eacute;todos para iniciar y terminar la aplicaci&oacute;n,
 * obtener la referencia a la ventana principal y al &aacute;rea de notificaci&oacute;n.
 * @author <a href="mailto:tezequiel@gmail.com">Ezequiel Turovetzky</a>
 * @version 1.0
 */
public class Application {
	
    //~ Static fields/initializers ---------------------------------------------

	/** Logger */
	private static final Log logger = LogFactory.getLog(Application.class);
    /** Singleton */
    private static Application instance;
    /** Ventana de la aplicacion */
    private MainWindow window;

    //~ Constructors -----------------------------------------------------------

    /**
     * Crea una nueva instancia de esta clase.
     */
    private Application() {
        window = new MainWindow();
    }

    //~ Methods ----------------------------------------------------------------

    /**
     * Devuelve la instancia de esta clase.
     * @return Instancia de esta clase.
     */
    public static Application getInstance() {
        if(instance == null) {
        	logger.debug("Iniciando Botonera...");
            instance = new Application();
        }
        return instance;
    }
    
    /**
     * Devuelve el logger de la aplicaci&oacute:n.
     * @return El logger de la aplicaci&oacute:n.
     */
    public static Log getLogger() {
    	return logger;
    }
    
    /**
     * Devuelve la ventana de la aplicacion.
     * @return La ventanad de la aplicacion.
     */
    public MainWindow getWindow() {
    	return window;
    }
    
    /**
     * Devuelve el &iacute;cono del &aacute;rea de notificaci&oacute;n
     * @return El &iacute;cono del &aacute;rea de notificaci&oacute;n
     */
    public BotoneraTray getTray() {
    	try {
    		return (BotoneraTray)SystemTray.getSystemTray().getTrayIcons()[0];
    	} catch(UnsupportedOperationException e) {
    		ExceptionHandlerFactory.getExceptionHandler().handle(e);
    	}
    	return null;
    }
    
    /**
     * Lanza la aplicaci&oacute;n.
     */
    public void start() {
        window.open();
    }

    /**
     * Termina la aplicaci&oacute;n.
     */
    public void quit() {
    	logger.debug("Finalizando Botonera...");
        window.dispose();
        System.exit(0);
    }
}